#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from job import job_add
from .data import(
    etfOHLC,
    storeOHLC,
    scheme_contract
) 
from job import job_update
# 策略基类
class Scheme:
    id = 0
    def __init__(self):
        pass
    # 返回交易日列表
    def setup(self):
        print("策略初始化")
        return [{"date":"0"},{"date":"1"},{"date":"2"},{"date":"3"}]
    # 每个交易日开盘，返回挂单
    def willopen(self,index,item):
        print("开盘了！买定离手",str(index),str(item))
        return [0,1]
    def exchange(self,index,item,order):
        # ETF 买入
        if order["type"]==1 and order["dir"]==0:
            code = order["code"]
            oprice = order["price"]
            ocount = order["count"]
            ohlc = etfOHLC(code=code,date=item)
            if oprice>ohlc["open"]:
                return ohlc["open"]
            if oprice>ohlc["low"]:
                return oprice
            return 0
        # 股票买入
        if order["type"]==0 and order["dir"]==0:
            code = order["code"]
            oprice = order["price"]
            ocount = order["count"]
            ohlc = storeOHLC(code=code,date=item)
            if oprice>ohlc["open"]:
                return ohlc["open"]
            elif oprice>ohlc["low"]:
                return oprice
            return 0
        # ETF 卖出
        if order["type"]==1 and order["dir"]==1:
            code = order["code"]
            oprice = order["price"]
            ocount = order["count"]
            ohlc = etfOHLC(code=code,date=item)
            if oprice<ohlc["open"]:
                return ohlc["open"]
            if oprice<ohlc["high"]:
                return oprice
            return 0
        # 股票 卖出
        if order["type"]==0 and order["dir"]==1:
            code = order["code"]
            oprice = order["price"]
            ocount = order["count"]
            ohlc = storeOHLC(code=code,date=item)
            if oprice<ohlc["open"]:
                return ohlc["open"]
            if oprice<ohlc["high"]:
                return oprice
            return 0


    def close(self,orders):
        print("关门了",str(orders))
        return True
    def clear(self):
        print("结束了")

# 交易所
class Exchange:

    # 策略对象
    scheme = None
    jobid = None


    def __init__(self,scheme:Scheme=None):

        self.scheme = scheme
        self.jobid = job_add(self.scheme.id,name="scheme",state=0)
        
    def main(self):
        #状态 0 默认 1运行中  2完成退出 3异常退出
        job_update(self.scheme.id,state=1)
        datas = self.scheme.setup()
        # 遍历交易日
        for (index,item) in enumerate(datas):
            orders = self.scheme.willopen(index,item)
            for order in orders:
                price = self.scheme.exchange(index,item,order)
                if price>0:
                    order["price"] = price
                    self.contract(order,item)
                else:
                    orders.remove(order)
            self.scheme.close(orders)
        job_update(self.scheme.id,state=2)
    # 成交单
    def contract(self,order,date):
        print("成交：",str(self.scheme.id),str(order))
        scheme_contract(strategyId=self.scheme.id,date=date,order=order)


if __name__ == '__main__':
    ex = Exchange(scheme=Scheme())
    ex.main()   
       
